import java.io.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class MtBullerResort {
    private FileInputStream fis;
    private ObjectInputStream ois;
    private FileOutputStream fos;
    private ObjectOutputStream oos;
    private ArrayList<Accommodation> accommodations;
    private ArrayList<Customer> customers;
    private ArrayList<TravelPackage> packages;

    public MtBullerResort() {
        accommodations = new ArrayList<>();
        customers = new ArrayList<>();
        packages = new ArrayList<>();
        // 加默认用户
        customers.add(new Customer(1,"aa","intermediate"));
        customers.add(new Customer(2,"bb","beginner"));
        customers.add(new Customer(3,"cc","expert"));
        // 加默认房间>10
        accommodations.add(new Accommodation(1, "Hotel", 100.0));
        accommodations.add(new Accommodation(2, "Lodge", 80.0));
        accommodations.add(new Accommodation(3, "Apartment", 120.0));
        accommodations.add(new Accommodation(4, "Hotel", 100.0));
        accommodations.add(new Accommodation(5, "Hotel", 100.0));
        accommodations.add(new Accommodation(6, "Hotel", 100.0));
        accommodations.add(new Accommodation(7, "Apartment", 120.0));
        accommodations.add(new Accommodation(8, "Hotel", 120.0));
        accommodations.add(new Accommodation(9, "Apartment", 120.0));
        accommodations.add(new Accommodation(10, "Lodge", 80.0));
        accommodations.add(new Accommodation(11, "Lodge", 100.0));
    }
    // 1、显示所有住宿
    public ArrayList<Accommodation> getAllAccommodations() {
        return new ArrayList<>(accommodations);
    }

    public ArrayList<Accommodation> getAvailableAccommodations() {
        ArrayList<Accommodation> availableAccommodations = new ArrayList<>(accommodations);
        for (TravelPackage travelPackage : packages) {
            availableAccommodations.remove(travelPackage.getAccommodation());
        }
        return availableAccommodations;
    }

    // 3、加个用户
    public void addCustomer(Customer customer) {
        customers.add(customer);
    }

    public ArrayList<Customer> getCustomers() {
        return customers;
    }
    public ArrayList<TravelPackage> getPackages() {
        return packages;
    }
    public void addPackage(int customerId, String startDateStr, int duration, int accommodationId) {
        Accommodation accommodation = findAccommodationById(accommodationId);
        System.out.println(accommodation);
        if (accommodation != null) {
            TravelPackage travelPackage = new TravelPackage(customerId, startDateStr, duration, accommodation);
            packages.add(travelPackage);
        } else {
            throw new IllegalArgumentException("Accommodation ID not found.");
        }
    }
    private Accommodation selectAccommodation() {
        // For simplicity, we assume the first available accommodation is selected
        for (Accommodation accommodation : accommodations) {
            if (accommodation.isAvailable()) {
                accommodation.setAvailable(false);
                return accommodation;
            }
        }
        return null;
    }
    // 5、加个订单，给客户定住宿


    // 6、列出所有订单
    public void listPackages() {
        System.out.println("List of Packages:");
        for (TravelPackage travelPackage : packages) {
            System.out.println(travelPackage.toString());
        }
    }

    // 根据id找住宿
    private Accommodation findAccommodationById(int id) {
        for (Accommodation accommodation : accommodations) {
            if (accommodation.getId() == id) {
                return accommodation;
            }
        }
        return null;
    }
    private Customer findCustom(int id){
        for (Customer cus : customers){
            if (cus.getId()==id){
                return cus;
            }
        }
        return null;
    }
    // 7、订单加lift passes
    public void addLiftPassToPackage(int packageId, int count) {
        TravelPackage travelPackage = findPackageById(packageId);
        if (travelPackage != null) {
            travelPackage.setLiftPasses();
            System.out.println(travelPackage);
        }
    }

    // 8、课程费用加到订单中
    public void addLessonsToPackage(int packageId, int count) {
        TravelPackage travelPackage = findPackageById(packageId);
        if (travelPackage != null) {
            travelPackage.addLessons(count);
        }
    }
    // 公用方法
    private TravelPackage findPackageById(int id) {
        for (TravelPackage travelPackage : packages) {
            if (travelPackage.getId() == id) {
                return travelPackage;
            }
        }
        return null;
    }

    // 910存文件

    public void savePackage() {
        try {
            fos = new FileOutputStream("package.dat");
            oos = new ObjectOutputStream(fos);
            for (TravelPackage travelPackage :packages) {
                oos.writeObject(travelPackage);
            }
            fos.close();
            oos.close();
            System.out.println("successfully");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void readPackage() {
        packages.clear();
        try {
            fis = new FileInputStream("package.dat");
            ois = new ObjectInputStream(fis);

            while (true) {
                try {
                    Object object = ois.readObject();
                    TravelPackage travelPackage = (TravelPackage)object;
                    packages.add(travelPackage);
                    System.out.println(travelPackage);
                } catch (EOFException eof) {
                    fis.close();
                    ois.close();
                    break;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

